Ext.define("App.datasource.view.SourceTableEditor", {
    extend: 'Extend.window.FormWindow',
    alias: ['widget.SourceTableEditor'],
    requires: ['App.report.model.KpiModel',
        'App.iteration.field.IterationComboBox',
        'App.reportconfiguration.field.ReportConfigurationComboBox',
        'App.datasource.model.SourceDataModel'],
    config: {
        // unmaskable: false,
        entity: 'entity',
        defaults: {
            columnWidth: 1
        },
        window: {
            title: '报表填写',
            width: 950,
            height: 600,
            resizable: false
        }
    },
    items: [{
        xtype: 'hidden',
        name: 'id'
    }, {
        xtype: 'hidden',
        name: 'projectId'
    }, {
        xtype: 'hidden',
        name: 'tableName'
    }, {
        xtype: 'IterationComboBox',
        name: 'iterationId',
        fieldLabel: '迭代',
        allowBlank: false,
        blankText: '迭代为必填字段，不允许为空！',
        readOnly: true
    }],
    loadRecord: function (record) {
        var me = this;
        var fieldOpts = {
            xtype: 'SourceDataGridField',
            name: 'datas',
            fieldLabel: '数据录入',
            columnWidth: 1,
            height: 370
        };
        if (me.targetGrid.dataTable.type == 'project') {
            this.down('IterationComboBox').hide();
            this.down('IterationComboBox').disable();
            fieldOpts.height = 400;
            delete fieldOpts.fieldLabel;
        }
        me.add(Ext.create(fieldOpts));
        me.callParent(arguments);
        if (Ext.isEmpty(record.get('id'))) {
            me.down('IterationComboBox').setReadOnly(false);
        }
        me.loadDataTable(record)
    },
    loadDataTable: function (record) {
        var me = this;
        if (Ext.isEmpty(app.project)) {
            return;
        }

        var deleteDatas = function (record, tableFields) {
            var datas = record.get('datas');
            if (!Ext.isEmpty(datas)) {
                for (var i = datas.length - 1; i >= 0; i--) {
                    var data = datas[i];
                    var cfield = data.field;
                    var finded = false;
                    for (var j = 0; j < tableFields.length; j++) {
                        if (cfield.id == tableFields[j].id) {
                            finded = true;
                            break;
                        }
                    }
                    if (finded == false) {
                        Ext.Array.remove(datas, data);
                    }
                }
            }
        }

        var addDatas = function (record, tableFields) {
            var datas = record.get('datas');
            if (Ext.isEmpty(datas)) {
                datas = [];
                record.set('datas', datas);
            }
            if (!Ext.isEmpty(tableFields)) {
                for (var i = 0; i < tableFields.length; i++) {
                    var field = tableFields[i];
                    var finded = false;
                    for (var j = 0; j < datas.length; j++) {
                        var data = datas[j];
                        var cfield = data.field;
                        if (cfield.id == field.id) {
                            var cr = me.down('gridfield[name="datas"]').getStore().findRecord('id', data.id);
                            cr.set('field', field);

                            inputType = Ext.isEmpty(field.projectCollectionField) ? null : field.projectCollectionField.inputType;

                            if (inputType == 1) {
                                cr.set('collectedValue', null)
                            } else {
                                if (inputType == 2) {
                                    cr.set('inputValue', null)
                                }
                            }

                            finded = true;
                            break;
                        }
                    }
                    if (finded == false) {
                        var record = Ext.create(me.down('gridfield[name="datas"]').model, {field: field, inputValue: "", collectedValue: ''});
                        record.isNew = true;
                        me.down('gridfield[name="datas"]').insertRecord(i, record, true);
                    }
                }
            }
        }
        // me.mask('初始化...');
        Ext.Ajax.request({
            url: 'data_table/query_referenced_data_tables_by_project',
            params: {projectId: app.project.id, tableName: record.get('tableName')},
            success: function (resp) {
                var tableFields = resp.result.data[record.get('tableName')].tableFields;
                if (tableFields != null) {
                    deleteDatas(record, tableFields);
                    addDatas(record, tableFields);
                } else {
                    record.set("datas", []);
                }
                me.unmask();
            },
            failure: function () {
                me.unmask();
            }
        })
    }
})


Ext.define('App.datasource.view.SourceDataGridField', {
    alias: ['widget.SourceDataGridField'],
    extend: 'Extend.form.field.GridField',
    submitFields: ['id', 'inputValue', 'collectedValue', 'field.id'],
    roweditable: true,
    tbar: {add: {hidden: true}, remove: {hidden: true}}, border: false,
    model: 'App.datasource.model.SourceDataModel',
    columns: [{
        header: '指标名称',
        dataIndex: 'fieldName',
        renderer: function (v, m, r) {
            if (r.get('abandoned') == true) {
                return "<span style='background-color: red;'>" + v + "</span>"
            }
            return v;
        }
    }, {
        header: '录入值',
        dataIndex: 'inputValue', editor: {xtype: 'textfield', itemId: 'inputValue'}
    }, {
        header: '采集值',
        dataIndex: 'collectedValue', editor: {xtype: 'textfield', itemId: 'collectedValue'}
    }],
    listeners: {
        startedit: function (rowedit, e, eOpts) {
            var record = e.record;
            var field = record.get('field');
            inputType = Ext.isEmpty(field.projectCollectionField) ? null : field.projectCollectionField.inputType;
            var cvi = rowedit.editor.down('textfield[itemId="collectedValue"]');
            var ivi = rowedit.editor.down('textfield[itemId="inputValue"]');
            cvi.setReadOnly(true);
            ivi.setReadOnly(true);
            if (field.dataType == 'string' || Ext.isEmpty(inputType) || inputType == 1) {
                ivi.setReadOnly(false);
                cvi.setValue();
            } else {
                if (inputType == 2) {
                    ivi.setReadOnly(true);
                    ivi.setValue();
                    cvi.setReadOnly(true);
                } else {
                    cvi.setReadOnly(true);
                    ivi.setReadOnly(false);
                }
            }
        }
    }
})
